package com.examsystem.service.impl;

import java.util.Date;
import java.util.List;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.examsystem.common.ResultJson;
import com.examsystem.common.StatusAndMsg;
import com.examsystem.dao.exame_userMapper;
import com.examsystem.dao.exame_user_subjectMapper;
import com.examsystem.entity.exame_user;
import com.examsystem.entity.exame_userExample;
import com.examsystem.entity.exame_userExample.Criteria;
import com.examsystem.entity.exame_user_subject;
import com.examsystem.entity.exame_user_subjectExample;
import com.examsystem.service.UserService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;

@Service
public class UserServiceImpl implements UserService {

	@Autowired
	private exame_userMapper userMapper;
	
	@Autowired
	private exame_user_subjectMapper usMapper;

	/**
	 * 用户登录，根据username，password查找
	 * 
	 * @param user
	 * @return
	 */
	@Override
	public ResultJson selectByUserAndPwd(exame_user user) {
		ResultJson result = null;
		// 获取用户和密码
		String username = user.getUsername();
		String password = user.getPassword();
		// 构造查询条件
		exame_userExample example = new exame_userExample();
		Criteria createCriteria = example.createCriteria();
		createCriteria.andUsernameEqualTo(username);
		createCriteria.andPasswordEqualTo(password);
		// 操作数据库
		List<exame_user> list = userMapper.selectByExample(example);
		if (null == list || list.size() == 0) {
			result = new ResultJson(StatusAndMsg.LOGINERROR.getStatus(), StatusAndMsg.LOGINERROR.getMsg(), null);
		} else {
			exame_user user2 = list.get(0);
			user2.setPassword("******");
			result = new ResultJson(StatusAndMsg.LOGINSUCCESS.getStatus(), StatusAndMsg.LOGINSUCCESS.getMsg(), user2);
		}

		return result;
	}

	// 查询用户（分页）
	public ResultJson selectUserList(Integer page, Integer pageSize) {
		ResultJson result = null;
		// 操作数据库
		PageHelper.startPage(page, pageSize);
		List<exame_user> list = userMapper.selectByExample(null);
		if (null == list || list.size() == 0) {
			// 为空
			result = new ResultJson(StatusAndMsg.NODATA.getStatus(), StatusAndMsg.NODATA.getMsg(), null);
		} else {
			PageInfo<exame_user> pageInfo = new PageInfo<>(list, pageSize);
			result = new ResultJson(StatusAndMsg.YES.getStatus(), StatusAndMsg.YES.getMsg(), pageInfo);
		}
		return result;
	}

	// 模糊查询用户（分页）
	public ResultJson selectUserByExample(exame_user user, String Sort, Integer page, Integer pageSize) {
		ResultJson result = null;
		// 构造查询条件
		exame_userExample example = new exame_userExample();
		if ("DESC".equals(Sort) || "ASC".equals(Sort)) {
			// 将创建时间进行排序
			example.setOrderByClause("createTime" + " " + Sort);
		}
		Criteria criteria = example.createCriteria();
		if (StringUtils.isNotBlank(user.getUsername())) {
			// 根据主键查询（唯一）
			criteria.andUsernameEqualTo(user.getUsername());
		} else {
			if (StringUtils.isNotBlank(user.getRealname())) {
				// 真实姓名
				criteria.andRealnameLike("%"+user.getRealname()+"%");
			}
			if (StringUtils.isNotBlank(user.getType())) {
				// 用户类型
				criteria.andTypeEqualTo(user.getType());
			}
		}
		// 操作数据库
		PageHelper.startPage(page, pageSize);
		List<exame_user> list = userMapper.selectByExample(example);
		if (null == list || list.size() == 0) {
			// 为空
			result = new ResultJson(StatusAndMsg.NODATA.getStatus(), StatusAndMsg.NODATA.getMsg(), null);
		} else {
			PageInfo<exame_user> pageInfo = new PageInfo<>(list, pageSize);
			result = new ResultJson(StatusAndMsg.YES.getStatus(), StatusAndMsg.YES.getMsg(), pageInfo);
		}
		return result;
	}

	// 根据id修改用户信息
	public ResultJson updateByKey(exame_user user) {
		ResultJson result = null;
		// 判断主键
		if (null == user.getUsername()) {
			result = new ResultJson(StatusAndMsg.PARAMEERROR.getStatus(), StatusAndMsg.PARAMEERROR.getMsg(), null);
			return result;
		}
		// 操作数据库
		int i = userMapper.updateByPrimaryKeySelective(user);
		if (i > 0) {
			result = new ResultJson(StatusAndMsg.YES.getStatus(), StatusAndMsg.YES.getMsg(), null);
		} else {
			result = new ResultJson(StatusAndMsg.SERVICEERROR.getStatus(), StatusAndMsg.SERVICEERROR.getMsg(), null);
		}
		return result;
	}

	// 根据id删除用户信息
	public ResultJson deleteBykey(List<String> ids) {
		ResultJson result = null;
		exame_userExample example=new exame_userExample();
		Criteria criteria = example.createCriteria();
		criteria.andUsernameIn(ids);
		// 操作数据库
		int i = userMapper.deleteByExample(example);
		if (i > 0) {
			result = new ResultJson(StatusAndMsg.YES.getStatus(), StatusAndMsg.YES.getMsg(), null);
		} else {
			result = new ResultJson(StatusAndMsg.SERVICEERROR.getStatus(), StatusAndMsg.SERVICEERROR.getMsg(), null);
		}
		return result;
	}
	/**
	 * 添加用户
	 */
	@Override
	public ResultJson addUser(exame_user user) {
		ResultJson result=null;
		//补全信息
		user.setCreatetime(new Date());
		user.setUpdatetime(new Date());
		if(StringUtils.isBlank(user.getType())) {
			user.setType("2");//没有填就是学生（权限低）
		}
		if(StringUtils.isBlank(user.getPassword())) {
			user.setPassword("123456");//默认密码123456
		}
		int i = userMapper.insert(user);
		if(i>0) {
			result=new ResultJson(StatusAndMsg.YES.getStatus(), StatusAndMsg.YES.getMsg(), null);
		}else {
			result=new ResultJson(StatusAndMsg.SERVICEERROR.getStatus(), StatusAndMsg.SERVICEERROR.getMsg(), null);
		}
		return result;
	}
	
	/**
	 * 用户科目关联
	 */
	@Override
	public ResultJson user_subject(String username,List<Integer> stids) {
		ResultJson result=null;
		//判断(不能给管理员设置科目管理)
		exame_user user = userMapper.selectByPrimaryKey(username);
		if(null==user||"0".equals(user.getType())) {
			result=new ResultJson(StatusAndMsg.SERVICEERROR.getStatus(), "无法设置管理员与科目关联", null);
		}else {
			int i=0;
			exame_user_subjectExample example=new exame_user_subjectExample();
			com.examsystem.entity.exame_user_subjectExample.Criteria createCriteria = example.createCriteria();
			createCriteria.andUsernameEqualTo(username);
			//删除原来科目关联
			usMapper.deleteByExample(example);
			//添加科目关联
			for(Integer stid:stids) {
				exame_user_subject us=new exame_user_subject();
				us.setUsername(username);
				us.setStid(stid);us.setCreatetime(new Date());us.setUpdatetime(new Date());
				usMapper.insert(us);
				i++;
			}
			if(i==stids.size()) {
				result=new ResultJson(StatusAndMsg.YES.getStatus(), StatusAndMsg.YES.getMsg(), null);
			}else {
				result=new ResultJson(StatusAndMsg.SERVICEERROR.getStatus(), StatusAndMsg.SERVICEERROR.getMsg(), null);
			}			
		}
		return result;
	}

}
